package cate.common.table.farm.udgpalace.sheet;

import cate.common.table.ESheet;
import cate.common.table.farm.udgpalace.row.UdgPalaceQuestionRow;
import cate.common.util.XT;

import java.util.List;
import java.util.stream.Collectors;

public class UdgPalaceQuestionSheet extends ESheet<UdgPalaceQuestionRow> {
    @Override
    public String getSheetName() {
        return "地宫探险问答和事件";
    }

    public int randomQuestionGroup(int type, int group) {
        List<Integer> questions = getList().stream().filter(e -> e.type == type && e.group == group)
                .mapToInt(e -> e.questionGroup).boxed().collect(Collectors.toList());
        if (questions.isEmpty()) {
            return 1;
        }
        return questions.get(XT.randomIndex(questions.size()));
    }

    public UdgPalaceQuestionRow getQuestionRow(int type, int group, int extraGroup, int chooseId) {
        if (chooseId == 0) {
            List<UdgPalaceQuestionRow> rows = getList().stream().filter(e -> e.type == type && e.group == group && e.questionGroup == extraGroup).collect(Collectors.toList());
            if (!rows.isEmpty()) {
                return rows.get(XT.randomIndex(rows.size()));
            }
        }
        return getList().stream().filter(e -> e.type == type && e.group == group && e.questionGroup == extraGroup && e.option == chooseId).findAny().orElse(null);
    }

    public UdgPalaceQuestionRow getCorrectRow(int type, int group, int extraGroup) {
        return getList().stream().filter(e -> e.type == type && e.group == group && e.questionGroup == extraGroup && e.correct).findAny().orElse(null);
    }
}
